﻿using System;
using System.Linq;
using System.Reflection;
using Autofac;
using Quartz;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.Tools;
using AutofacModel = Autofac.Module;

namespace ThresholdAssistant.Handler
{
    public class QuartzInject : AutofacModel
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            //用于实现属性注入的基础接口
            Type baseType = typeof(IJob);
            //用于实现属性注入的基础接口
            Type baseType1 = typeof(IBaseAutofac);

            //扫描当前执行目录下所有存在继承于IJob接口的程序集
            Assembly[] assemblyList = AssemblyUtil.GetAssemblyList(baseType);
            if (assemblyList != null && assemblyList.Length > 0)
            {
                //注入所有继承于IJob接口的实现类
                builder.RegisterAssemblyTypes(assemblyList).Where(type => type != baseType && baseType.IsAssignableFrom(type))
                       .AsSelf().InstancePerLifetimeScope();
            }
            //扫描当前执行目录下所有存在继承于IAutofacBase接口的程序集
            Assembly[] assemblyList1 = AssemblyUtil.GetAssemblyList(baseType1);
            if (assemblyList1 != null && assemblyList1.Length > 0)
            {
                //注入所有继承于IAutofacBase接口的实现类
                builder.RegisterAssemblyTypes(assemblyList1).Where(type => type != baseType1 && baseType1.IsAssignableFrom(type))
                   .AsSelf().InstancePerLifetimeScope().PropertiesAutowired();
            }
        }
    }
}
